#include "includes/parallelepiped.h"

void parallelepiped_init(Parallelepiped * p){
	
	p->list1 = glGenLists(4);
	p->list2 = p->list1 + 1;
	p->list3 = p->list2 + 1;
	p->list4 = p->list3 + 1;
	
	/* Face arriere */
	glNewList(p->list1, GL_COMPILE);
		glBegin(GL_QUADS);
			glTexCoord2i(0, 0);	glVertex3f(0., 0., 0.);
			glTexCoord2i(0, 1);	glVertex3f(0., 0., p->height);
			glTexCoord2i(1, 1);	glVertex3f(0., p->width, p->height);
			glTexCoord2i(1, 0);	glVertex3f(0., p->width, 0.);
		glEnd();
	glEndList();
	
	/* Face droite */
	glNewList(p->list2, GL_COMPILE);
		glBegin(GL_QUADS);
			glTexCoord2i(0,0);	glVertex3f(0., p->width, 0.);
			glTexCoord2i(0,1);	glVertex3f(0., p->width, p->height);
			glTexCoord2i(1,1);	glVertex3f(p->length, p->width, p->height);
			glTexCoord2i(1,0);	glVertex3f(p->length, p->width, 0.);
		glEnd();
	glEndList();
	
	/* Face avant */
	glNewList(p->list3, GL_COMPILE);
		glBegin(GL_QUADS);
			glTexCoord2i(0,0);	glVertex3f(p->length, p->width, 0.);
			glTexCoord2i(0,1);	glVertex3f(p->length, p->width, p->height);
			glTexCoord2i(1,1);	glVertex3f(p->length, 0., p->height);
			glTexCoord2i(1,0);	glVertex3f(p->length, 0., 0.);
		glEnd();
	glEndList();
	
	/* Face gauche */
	glNewList(p->list4, GL_COMPILE);
		glBegin(GL_QUADS);
			glTexCoord2i(0,0);	glVertex3f(p->length, 0., 0.);
			glTexCoord2i(0,1);	glVertex3f(p->length, 0., p->height);
			glTexCoord2i(1,1);	glVertex3f(0., 0., p->height);
			glTexCoord2i(1,0);	glVertex3f(0., 0., 0.);
		glEnd();
	glEndList();
	
}

void parallelepiped_draw(Parallelepiped * p){
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glEnable(GL_TEXTURE_2D);
	
	glBindTexture(GL_TEXTURE_2D, p->texture1);
	glCallList(p->list1);
	glBindTexture(GL_TEXTURE_2D, p->texture2);
	glCallList(p->list2);
	glBindTexture(GL_TEXTURE_2D, p->texture3);
	glCallList(p->list3);
	glBindTexture(GL_TEXTURE_2D, p->texture4);
	glCallList(p->list4);
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	
}
